<template>
    <div class="app-container">
      <nabars index="7"></nabars>
      <div class="linebtn" v-loading="loadings" style="margin-top: 15px;">
        <div class="btn" v-for="item,index in name" :key="index" @click="setIdx(index)" :class="idx==index?'select':''">{{ item.dictName }}</div>
      </div>
      <div v-if="name!=''&&name!=null">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px" class="frominfo">
          <!-- 搜索栏部分 -->
          <div class="from-item">
            <el-form-item label="标签名称" prop="dictLabel">
              <el-input v-model="queryParams.dictLabel" @keyup.enter.native="handleQuery" placeholder="请输入标签名称"
                clearable />
            </el-form-item>
          </div>
          <div class="from-Btn">
            <el-form-item>
                <el-button class="blue-button" type="primary" icon="el-icon-search" size="mini"
                  @click="handleQuery">查询</el-button>
            </el-form-item>
            <el-form-item>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </div>
        </el-form>
        <!-- 导出按钮 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button class="blue-button" @click="handleAdd" plain icon="el-icon-plus" size="mini" style="background-color: rgb(0, 120, 251);color: rgb(255, 255, 255);border-color: rgb(0, 120, 251);">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button class="blue-button" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" style="background-color: rgb(0, 120, 251);color: rgb(255, 255, 255);border-color: rgb(0, 120, 251);">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button class="blue-button" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" style="background-color: rgb(0, 120, 251);color: rgb(255, 255, 255);border-color: rgb(0, 120, 251);">删除</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" :header-cell-style="{ background: '#fcfcfd', color: '#000' }" :border="false">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="标签名称" align="center" prop="dictLabel">
            <template slot-scope="scope">
              <span v-if="scope.row.listClass == '' || scope.row.listClass == 'default'">{{scope.row.dictLabel}}</span>
              <el-tag v-else :type="scope.row.listClass == 'primary' ? '' : scope.row.listClass">{{scope.row.dictLabel}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="标签键值" align="center" prop="dictValue" />
          <el-table-column label="字典排序" align="center" prop="dictSort" />
          <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:dict:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:dict:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

    
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

        <!-- 添加或修改参数配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="标签名称" prop="dictLabel">
              <el-input v-model="form.dictLabel" placeholder="请输入标签名称" />
            </el-form-item>
            <el-form-item label="标签键值" prop="dictValue">
              <el-input v-model="form.dictValue" placeholder="请输入标签键值" />
            </el-form-item>
            <el-form-item label="显示排序" prop="dictSort">
              <el-input-number v-model="form.dictSort" controls-position="right" :min="0" />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </template>
    
  <script>
  import request from '@/utils/request'
  import nabars from '../components/nabar.vue'
  export default {
    dicts: ['sys_friendlabel_type'],
    components: {
        nabars,
    },
    data() {
      return {
        idx:0,
        name:[],
        loadings:true,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        dataList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          dictType: null,
          dictLabel: null,
        },
        // 表单参数
        form: {
          dictType: 'group_label',
          dictLabel: null,
          dictValue:null,
          dictSort:null,
          remark:null
        },
        // 表单校验
        rules: {
          dictLabel: [
            { required: true, message: "标签名称不能为空", trigger: "blur" }
          ],
          dictValue: [
            { required: true, message: "标签键值不能为空", trigger: "blur" }
          ],
          dictSort: [
            { required: true, message: "数据顺序不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.optionselect().then((res) => {
        this.getList();
      })
    },
    methods: {
      setIdx(idx){
        this.idx=idx
        this.queryParams.dictType=this.name[idx].dictType
        this.form.dictType=this.name[idx].dictType
        this.getList();
      },
      async optionselect(){
        this.loadings=true
        await optionselect().then(res=>{
            if(res.code==200){
              if(res.data){
                this.name=res.data
                this.queryParams.dictType=res.data[0].dictType
                this.form.dictType=res.data[0].dictType
              }
              this.loadings=false
            }
        })
      },
      /** 查询微信好友信息列表 */
     async getList() {
        this.loading = true;
         getDictList(this.queryParams).then(res => {
          if (res['code']==200) {
            this.total = res['total'];
            this.dataList = res['rows'];
          }
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          dictType: this.name[this.idx].dictType,
          dictLabel: null,
          dictValue:null,
          dictSort:null,
          remark:null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.dictCode)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加标签信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        console.log(row)
        this.reset();
        const id = row.dictCode || this.ids;
        getInfo(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改标签信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.dictCode != undefined) {
              updateList(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addList(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.dictCode || this.ids;
        this.$modal.confirm('是否确认删除所选数据项？').then(function() {
          return delInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
    }
  };
  //获取tab项
  export function optionselect(query) {
    return request({
      url: '/label/relevance/optionselect',
      method: 'get',
      data: query
    })
  }
  //获取tab类型列表
  export function getDictList(query) {
    return request({
      url: '/label/relevance/list',
      method: 'get',
      params: query
    })
  }
  //新增
  export function addList(query) {
    return request({
      url: '/label/relevance',
      method: 'post',
      data: query
    })
  }
  //详情
  export function getInfo(id) {
    return request({
      url: '/label/relevance/'+id,
      method: 'get',
    })
  }
  //修改
  export function updateList(query) {
    return request({
      url: '/label/relevance',
      method: 'PUT',
      data: query
    })
  }
  //删除
  export function delInfo(id) {
    return request({
      url: '/label/relevance/'+id,
      method: 'DELETE',
    })
  }
  </script>
  <style lang="scss">
  .app-container {
      background-color: #f6f9fb;
  }
  .linebtn {
      display: flex;
      margin: -10px 0 10px 5px;
      height: 38px;
    
      .btn {
        width: 110px;
        text-align: center;
        line-height: 38px;
        font-size: 14px;
        color: #8c8c8c;
        margin-right: 10px;
        border: 1px solid #eaeff2;
        background-color: #fff;
        border-radius: 5px;
        cursor: pointer;
        .link-type {
          color: #8c8c8c;
        }
      }
    
      .select {
        background-color: #eaf6ff;
        border: 1px solid #288dfc;
        color: #288dfc;
        font-weight: 550;
      }
    }
    .frominfo {
      display: flex;
      background-color: #fff;
      padding: 10px;
      padding-bottom: 0;
      margin-bottom: 20px;
    
      .from-item {
        display: flex;
        flex-wrap: wrap;
        width: 85%;
      }
    
      .from-Btn {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        width: 20%;
      }
    }
  </style>